﻿using CaseManagerLibrary.Http;
using CaseManagerLibrary.Models;
using PSDLib.Models;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PSDManager.SystemSettings.DeviceTree
{
    public partial class FrmChannelSelector : Form
    {
        public event OnCameraSelectedEventHanlder CameraSelected;

        private DeviceTreeDH8900 deviceTreeDH8900 = null;
        private DeviceTreeDaHua deviceTreeDaHua = null;
        private DeviceTreeHK deviceTreeHK = null;

        private Label currSettingItem;
        private Dictionary<string, IDeviceTree> settingPanels = new Dictionary<string, IDeviceTree>();
        public FrmChannelSelector()
        {
            InitializeComponent();

            Load += FrmChannelSelector_Load;
        }

        private void FrmChannelSelector_Load(object sender, EventArgs e)
        {
            Init();
        }

        public void Init()
        {
            deviceTreeDH8900 = new DeviceTreeDH8900();
            deviceTreeDH8900.Dock = DockStyle.Fill;
            palDeviceTree.Controls.Add(deviceTreeDH8900);
            deviceTreeDH8900.BringToFront();
            deviceTreeDH8900.CameraSelected += DeviceTree_CameraSelected;
            settingPanels.Add(miDH8900.Name, deviceTreeDH8900);

            deviceTreeDaHua = new DeviceTreeDaHua();
            deviceTreeDaHua.Dock = DockStyle.Fill;
            palDeviceTree.Controls.Add(deviceTreeDaHua);
            deviceTreeDaHua.BringToFront();
            deviceTreeDaHua.CameraSelected += DeviceTree_CameraSelected;
            settingPanels.Add(miDaHua.Name, deviceTreeDaHua);

            deviceTreeHK = new DeviceTreeHK();
            deviceTreeHK.Dock = DockStyle.Fill;
            palDeviceTree.Controls.Add(deviceTreeHK);
            deviceTreeHK.BringToFront();
            deviceTreeHK.CameraSelected += DeviceTree_CameraSelected;
            settingPanels.Add(miHK.Name, deviceTreeHK);

            var result_server = HttpHelper.Get<HResult<List<dl_server_config>>>(AppSettings.Setting.API.GetDownloadServerConfig);
            if (result_server.success)
            {
                deviceTreeDH8900.Init(result_server.data);
                deviceTreeDaHua.Init(result_server.data);
                deviceTreeHK.Init(result_server.data);
            }

            SetCurrentSetting(miDH8900);
        }

        private void DeviceTree_CameraSelected(PSDLib.Download.ChannelInfo channel, dl_server_config server)
        {
            if (CameraSelected != null)
            {
                CameraSelected(channel, server);
            }
        }

        private void SetCurrentSetting(Label miSetting)
        {
            if (currSettingItem != null)
            {
                currSettingItem.BackColor = Color.Transparent;
            }

            currSettingItem = miSetting;

            currSettingItem.BackColor = Color.LightSteelBlue;

            (settingPanels[currSettingItem.Name] as Control).BringToFront();
        }

        private void MiSettings_MouseEnter(object sender, EventArgs e)
        {
            if (sender != currSettingItem)
            {
                (sender as Label).BackColor = Color.PowderBlue;
            }
        }

        private void MiSettings_MouseLeave(object sender, EventArgs e)
        {
            if (sender != currSettingItem)
            {
                (sender as Label).BackColor = Color.Transparent;
            }
        }

        private void MiSettings_Click(object sender, EventArgs e)
        {
            SetCurrentSetting((sender as Label));
        }

        private void btnAccept_Click(object sender, EventArgs e)
        {
            (settingPanels[currSettingItem.Name] as IDeviceTree).Accept();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
